﻿#include "clock.h"
#include <QTime>
#include <QPainter>
#include <QFont>
#include <QPoint>
#include <QPen>
Clock::Clock(QWidget *parent) : QWidget(parent)
{
    m_hourColor = Qt::red;
    m_minuteColor = Qt::green;
    m_secondColor = Qt::blue;

    m_timer = new QTimer(this);
    (void)connect(m_timer, &QTimer::timeout, this, QOverload<>::of(&Clock::update));
    m_timer->start(1000);
}

Clock::~Clock()
{
}

void Clock::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    // 将坐标原点移到中心
    int size = qMin(width(), height());
    painter.setRenderHint(QPainter::Antialiasing);

    painter.translate(width() / 2, height() / 2);
    painter.scale(size / 200, size / 200);

    // 获取当前时间
    QTime time = QTime::currentTime();

    //设置透明度
    painter.setOpacity(0.8);
    // 画秒针
    drawSecond(painter, time);
    // 画分针
    drawMinute(painter, time);
    // 画时针
    drawHour(painter, time);

    //设置透明度
    painter.setOpacity(1);
    // 画表盘
    drawClock(painter);
}
void Clock::drawClock(QPainter &painter)
{
    // 画小时数字
    painter.save();
    painter.setFont(QFont("Arial", 8));
    for(int i = 1; i < 13; i++)
    {
        painter.rotate(30.0);
        painter.drawText(QRect(-15, -117, 30, 20), Qt::AlignCenter, QString::number(i));
    }
    painter.restore();
    // 画小时刻度
    painter.save();
    painter.setPen(m_hourColor);
    for(int i = 0; i < 12; ++i)
    {

        painter.drawLine(88, 0, 96, 0);
        painter.rotate(30.0);
    }
    painter.restore();
    // 画分钟刻度
    painter.save();
    painter.setPen(m_minuteColor);
    for (int j = 0; j < 60; ++j)
    {
        if ((j % 5) != 0)
            painter.drawLine(92, 0, 96, 0);
        painter.rotate(6.0);
    }
    painter.restore();

}

void Clock::drawSecond(QPainter &painter, const QTime &time)
{
    // 画秒针 -- 秒针为三角形
    // 秒针的三个点
    static const QPoint secondHand[3] = {
        QPoint(3, 8),       // 左上
        QPoint(-3, 8),      // 右上
        QPoint(0, -70)      // 中间
    };
    
    painter.save();
    painter.setPen(m_secondColor);
    painter.setBrush(m_secondColor);
    painter.rotate(6.0 * time.second());
    painter.drawConvexPolygon(secondHand, 3);
    painter.restore();
}

void Clock::drawMinute(QPainter &painter, const QTime &time)
{
    // 画分针 -- 分针为三角形
    // 分针的三个点
    static const QPoint minuteHand[3] = {
        QPoint(5, 8),       // 左上
        QPoint(-5, 8),      // 右上
        QPoint(0, -50)      // 中间
    };
    painter.save();
    painter.setPen(m_minuteColor);
    painter.setBrush(m_minuteColor);
    painter.rotate(6.0 * time.minute());
    painter.drawConvexPolygon(minuteHand, 3);
    painter.restore();
}

void Clock::drawHour(QPainter &painter, const QTime &time)
{
    // 画时针 -- 时针为三角型
    static const QPoint hourHand[3] = {
        QPoint(7, 8),       // 左上
        QPoint(-7, 8),      // 右上
        QPoint(0, -30)      // 中间
    };

    painter.save();
    painter.setPen(m_hourColor);
    painter.setBrush(m_hourColor);
    painter.rotate(30.0 * (time.hour() % 12 + time.minute() / 60.0));
    painter.drawConvexPolygon(hourHand, 3);
    painter.restore();
}
